import { Component, OnInit } from '@angular/core';
import { BladeContainerBase } from "../../../../shared/blade/blade-container-base";
import { BladeData } from "../../../../shared/blade/blade-data";
import { Blade } from "../../../../shared/blade/blade.model";
import { Command } from "../../../../shared/toolbar.service";
import { DialogResult } from "../../../../shared/dialog.service";
import { Observable } from "rxjs/Observable";
import { Setofkey } from "../../../../entity/setofkey.model";
import { SetofkeyService } from "../../../../service/setofkey.service";
import { Tag } from "../../../../entity/tag.model";

//状态组编辑component
import { MatrixStateGroupEditComponent } from "../matrix-state-group-edit/matrix-state-group-edit.component"
import { MatrixStateGroup } from "../../../../entity/matrix-state-group.model";
import { MatrixStateGroupService } from "../../../../service/matrix-state-group.service";

//状态list
import { MatrixStateListComponent } from "../matrix-state-list/matrix-state-list.component"
import { MatrixState } from "../../../../entity/matrix_state.model";


@Component({
    selector: 'app-matrix-state-group-list',
    templateUrl: './matrix-state-group-list.component.html',
    styleUrls: ['./matrix-state-group-list.component.css']
})
export class MatrixStateGroupListComponent extends BladeContainerBase<MatrixStateGroup> {

    public matrixStateGroups: MatrixStateGroup[] = [];
    private currentMatrixStateGroup: MatrixStateGroup;
    public original: MatrixStateGroup[] = [];

    private tagId: number;

    constructor(
        private matrixStateGroupService: MatrixStateGroupService,
    ) { super() }

    ngOnInit(): void {
        this.commands = [
            new Command("add", "添加", "fa fa-plus", "setofkey:insert", blade => this.exeAdd(blade), blade => this.canExeAdd(blade)),
            new Command("edit", "编辑", "fa fa-edit", "setofkey:update", blade => this.exeEdit(blade), blade => this.canExeEdit(blade)),
            new Command("delete", "删除", "fa fa-trash-o", "setofkey:delete", blade => this.exeDelete(blade), blade => this.canDelete(blade)),
        ];
        super.ngOnInit();
    }

    initData(): any | Observable<any> | Promise<any> {
        this.tagId = (this.parentEntity as Tag).tagId
        this.matrixStateGroupService.getMatrixStateGroupsByTagId(this.tagId)
            .then(res => {
                if (!res) { return false; };
                this.original = [...res];
                this.matrixStateGroups = [...this.original];
                if (this.matrixStateGroups.length > 0) {
                    this.currentMatrixStateGroup = this.matrixStateGroups[0]
                    this.showMatrixStateListBlade(this.currentMatrixStateGroup)
                }

            })
            .catch(error => this.bladeData.error = error)
            .then(res => this.bladeData.isLoading = false);

    }
    exeAdd(blend: Blade): void {
        let data: BladeData = new BladeData();
        data.id = "matrixStateGroupEdit";
        data.title = "新建矩阵状态组";
        data.isClosingDisabled = false;
        let createKeyBlade = this.bladeService.createBlade(MatrixStateGroupEditComponent, data, true, null, this.currentEntity);
        this.bladeService.showBlade(createKeyBlade, this.currentBlade);
    }
    canExeAdd(blend: Blade): boolean {
        return true;
    }
    exeEdit(blend: Blade): void {
        if (this.currentEntity != undefined) {
            let data: BladeData = new BladeData();
            data.id = "matrixStateGroupEdit";
            data.title = this.currentEntity.stateGroupName;
            let editMatrixStateGroupBlade = this.bladeService.createBlade(MatrixStateGroupEditComponent, data, false, this.currentEntity);
            this.currentBlade.bladeService.showBlade(editMatrixStateGroupBlade, this.currentBlade);
        }
    }
    canExeEdit(blend?: Blade): boolean {
        return this.currentEntity != null;
    }
    exeDelete(blend: Blade): void {
        let selectedStateGroup = this.getSelected();
        if (selectedStateGroup.length > 0) {
            this.dialogService
                .showConfirmationDialog("确认删除", "是否要删除选中项？")
                .subscribe(dialogResult => {
                    if (dialogResult == DialogResult.Ok) {
                        this.currentBlade.bladeData.isLoading = true;
                        this.matrixStateGroupService.batchDelete(selectedStateGroup.map(o => o.stateGroupId))
                            .then(res => {
                                if (res) {
                                    this.refresh(undefined);
                                }
                            })
                            .catch(error => this.bladeData.error = error)
                            .then(res => this.bladeData.isLoading = false);
                    }
                })

        }

    }
    canDelete(blend?: Blade): boolean {
        return this.getSelected().length > 0;
    }

    onSeletedChange(matrixStateGroup: MatrixStateGroup) {
        if (this.currentBlade.childrenCanDeactivate()) {
            this.showMatrixStateListBlade(matrixStateGroup);
        }
    }

    //打开当前状态组下的状态列表
    showMatrixStateListBlade(selectedStateGroup: MatrixStateGroup) {
        if (this.currentEntity != selectedStateGroup
            || this.bladeService.findBlade("showMatrixStateList") == undefined) {

            let data: BladeData = new BladeData();
            data.id = "showMatrixStateList";
            data.title = selectedStateGroup.stateGroupName + "矩阵状态列表";
            data.isClosingDisabled = false;
            let matrixStateListBlade = this.bladeService.createBlade(MatrixStateListComponent, data, false, null, selectedStateGroup);
            this.bladeService.showBlade(matrixStateListBlade, this.currentBlade).then(res => {
                if (res) {
                    this.currentEntity = selectedStateGroup;
                }
            });
        }
    }

	/**
     * 单选
     * 
     * @param {any} event 
     * @param {any} key 
     * 
     * @memberof MatrixStateGroupListComponent
     */
    onItemCheack(event: HTMLInputElement, key: MatrixStateGroup) {
        key.isChecked = event.checked;
    }

	/**
     * 全选/全不选
     * 
     * @param {HTMLInputElement} event 
     * 
     * @memberof MatrixStateGroupListComponent
     */
    onAllCheack(event: HTMLInputElement) {
        if (this.matrixStateGroups.length > 0) {
            for (let i = 0; i < this.matrixStateGroups.length; i++) {
                this.matrixStateGroups[i].isChecked = event.checked;
            }
        }
    }
    /**
    * 获得选中项
    * 
    * @returns {Setofkey[]} 
    * 
    * @memberof MatrixStateGroupListComponent
    */
    getSelected(): MatrixStateGroup[] {
        if (this.matrixStateGroups.length > 0) {
            let filter = this.matrixStateGroups.filter(ref => ref.isChecked)
            return filter == null ? [] : filter;
        }
        return [];
    }

}



// WEBPACK FOOTER //
// ./src/app/module/basic/matrix-config/matrix-state-group-list/matrix-state-group-list.component.ts